﻿module Classes {
    ////定义个类
    //class Greeter1 {
    //    greeting: string;//属性
    //    constructor(message: string) {//构造函数
    //        this.greeting = message;
    //    }
    //    greeter() {//方法
    //        return "Hello " + this.greeting;
    //    }
    //}
    //var greeter1 = new Greeter1("world");//调用
    ////继承和多态
    //class Animal {
    //    private name: string;//默认为public 
    //    constructor(name: string) {
    //        this.name = name;
    //    }
    //    move(meters: number = 0) {
    //        alert(this.name + " moved " + meters + "m.");
    //    }
    //}
    //class Snake extends Animal {
    //    constructor(name: string) {
    //        super(name);
    //    }
    //    move(meters = 5) {
    //        alert('Slithering...');
    //        super.move(meters);
    //    }
    //}
    //class Horse extends Animal {
    //    constructor(name: string) {
    //        super(name);
    //    }
    //    move(meters = 45) {
    //        alert("Galloping...");
    //        super.move(meters);
    //    }
    //}
    //var sam = new Snake("Sammy the Python");
    //var tom: Animal = new Horse("");
    //sam.move();
    //tom.move(34);
    ////兼容类型
    ///*
    //    Typescript 是结构化类型系统。当我们比较2个类，
    //无论他们来自那，如果他们的每个成员兼容，那么他们就是
    //兼容类型。
    //    当2个类中有“private”成员时。我们需要区别对待(
    //因为private的不可见性)。
    //如果2个类有同private只有当他们来自于同一个最初定义的
    //基类。
    //*/
    //class Employee {
    //    name: string;
    //    constructor(name: string) { this.name = name; }
    //}
    //class Employee1 {
    //    name: string;
    //    constructor(name: string) { this.name = name; }
    //}
    //var an = new Employee("animal");
    //an = new Employee1("fds");
    ////

    //class Employee2 {
    //    private name: string;
    //    constructor(name: string) { this.name = name; }
    //}
    //class Employee3 extends Employee2 {
    //    constructor(name: string) { super(name); }
    //}
    //class Employee4 extends Employee2 {
    //    constructor(name: string) { super(name); }
    //}

    //var employee3 = new Employee3("animal");
    //employee3 = new Employee4("fds");
    ////在构造函数中定义和初始化成员变量
    //class Animal1 {
    //    constructor(private name: string) { }
    //    mov(meters: number) {
    //        alert(this.name + " moved " + meters + "m.");
    //    }
    //}
    ////类的访问器使用(get;set)
    //class Employee5 {//没有访问器的使用方式
    //    fullName; string
    //}
    //var employee5 = new Employee5();
    //employee5.fullName = "Bob Smith";
    //if (employee5.fullName) {
    //    alert(employee5.fullName);
    //}
    //访问修饰符
    var passcode = "secret passcode";
    class Employee6 {
        private _fullName: string;
        get fullName(): string {
            return this._fullName
        }
        set fullName(newname: string) {
            if (passcode && passcode == "secret passcode") {
                this._fullName = newname;
            }
            else {
                alert("Error:Unauthorized update of employee!");
            }
        }
    }
    var employee6 = new Employee6();
    employee6.fullName = "Bob Smith";
    if (employee6.fullName) {
        alert(employee6.fullName);
    }
    //静态属性
    class Grid {
        static origin = { x: 0, y: 0 };
        constructor(public scale: number) { }
        calculateDistanceFromOrigin(point: { x: number; y: number; }) {
            var xdist = (point.x - Grid.origin.x);
            var yDist = (point.y - Grid.origin.y);
            return Math.sqrt(xdist * xdist + yDist + yDist) /
                this.scale;
        }
    }
    var grid1 = new Grid(1.0);
    var grid2 = new Grid(5.0);
}